
package com.example.filetransfer.publicsource.fileutils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Dir implements Comparable<Dir> {
    private String dirName = null;
    private List<Dir> childDirs = new ArrayList<Dir>();
    private List<MyFile> childFiles = new ArrayList<MyFile>();

    public Dir(String name) {
        this.dirName = name;
    }

    public String dirName() {
        return dirName;
    }

    public void addChildDir(Dir dir) {
        childDirs.add(dir);
    }

    public void addChildFile(MyFile file) {
        childFiles.add(file);
    }

    public List<Dir> getChildDirs() {
        return childDirs;
    }

    public List<MyFile> getChildFiles() {
        return childFiles;
    }

    private void clearUpDirs(Dir dir) {
        Collections.sort(dir.childDirs);
        for (Dir child : dir.childDirs) {
            clearUpDirs(child);
        }
        Collections.sort(childFiles);
    }

    public void clearUp() {
        clearUpDirs(this);
    }

    public void dir2String(StringBuilder stringBuilder, Dir dir) {
        List<Dir> childDirs = dir.getChildDirs();
        List<MyFile> childFiles = dir.getChildFiles();
        for (Dir childDir : childDirs) {
            dir2String(stringBuilder, childDir);
            stringBuilder.append(childDir.dirName + "\n");
        }
        for (MyFile file : childFiles) {
            stringBuilder.append(file.getFilePath() + "\n");
        }
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        dir2String(stringBuilder, this);
        return stringBuilder.toString();
    }

    @Override
    public int compareTo(Dir o) {
        return dirName.compareTo(o.dirName);
    }
}
